﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.KhuyenMaiTangHang
{
    public partial class frmQuanLy : Form
    {
        #region Cap nhap giao diện main
        public delegate void GetBoolean(Boolean capnhat);
        public GetBoolean GetData;
        #endregion

        #region Delegate form xử lý
        public void GetValue(Boolean capnhat)
        {
            HienThi(Entitystatic.date.Date);
            if (!capnhat)
                Them();
        }

        public Entities.KhuyenMaiTangHang[] HienThi(DateTime date)
        {
            hienthi = Common.StaticVariable.InterfaceObject.SelectKhuyenMaiTangHang();
            foreach (Entities.KhuyenMaiTangHang item in hienthi)
            {
                if (date.Date > item.NgayKetThuc.Date)
                    item.GiaTriTrangThai = "Đã sử dụng";
                else if (date.Date < item.NgayBatDau.Date)
                    item.GiaTriTrangThai = "Sẽ sử dụng";
                else if (date.Date >= item.NgayBatDau.Date && date.Date <= item.NgayKetThuc.Date)
                    item.GiaTriTrangThai = "Đang sử dụng";
            }
            dgvHienThi.DataSource = hienthi;
            fix();
            return hienthi;
        }

        void fix()
        {
            for (int i = 0; i < dgvHienThi.ColumnCount; i++)
            {
                dgvHienThi.Columns[i].Visible = false;
            }
            dgvHienThi.ReadOnly = true;
            dgvHienThi.Columns[Common.Constants.KhuyenMaiTangHang.MaKhuyenMaiTangHang].Visible = true;
            dgvHienThi.Columns[Common.Constants.KhuyenMaiTangHang.TenDotKhuyenMai].Visible = true;
            dgvHienThi.Columns[Common.Constants.KhuyenMaiTangHang.NgayBatDau].Visible = true;
            dgvHienThi.Columns[Common.Constants.KhuyenMaiTangHang.NgayKetThuc].Visible = true;
            dgvHienThi.Columns[Common.Constants.KhuyenMaiTangHang.NgayLap].Visible = true;
            dgvHienThi.Columns[Common.Constants.KhuyenMaiTangHang.GiaTriTrangThai].Visible = true;
            dgvHienThi.Columns[Common.Constants.KhuyenMaiTangHang.MaKhuyenMaiTangHang].HeaderText = "Mã Khuyến Mại";
            dgvHienThi.Columns[Common.Constants.KhuyenMaiTangHang.TenDotKhuyenMai].HeaderText = "Tên Đợt Khuyến Mại";
            dgvHienThi.Columns[Common.Constants.KhuyenMaiTangHang.NgayBatDau].HeaderText = "Ngày Bắt Đầu";
            dgvHienThi.Columns[Common.Constants.KhuyenMaiTangHang.NgayKetThuc].HeaderText = "Ngày Kết Thúc";
            dgvHienThi.Columns[Common.Constants.KhuyenMaiTangHang.NgayLap].HeaderText = "Ngày Lập";
            dgvHienThi.Columns[Common.Constants.KhuyenMaiTangHang.GiaTriTrangThai].HeaderText = "Trạng Thái";
            dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvHienThi.AllowUserToAddRows = false;
            dgvHienThi.AllowUserToDeleteRows = false;
            dgvHienThi.AllowUserToResizeRows = false;
            dgvHienThi.RowHeadersVisible = false;
        }
        #endregion

        #region Khởi tạo
        bool checkClose = false;
        Entities.KhuyenMaiTangHang khuyenmaitanghang;
        public static Entities.KhuyenMaiTangHang[] hienthi;
        public static Entities.KhuyenMaiTangHang temp;

        public frmQuanLy()
        {
            InitializeComponent();
        }
        #endregion

        #region Check Permission
        public bool CheckPer(int hanhDong)
        {
            return Common.Utilities.Check(Common.FormName.QuanLyKhuyenMaiTangHang, hanhDong);
        }
        #endregion

        #region Thêm
        private void Them()
        {
            try
            {
                GUI.KhuyenMaiTangHang.frmXuLy frm = new GUI.KhuyenMaiTangHang.frmXuLy(hienthi);
                frm.GetData = new GUI.KhuyenMaiTangHang.frmXuLy.GetString(GetValue);
                
                frm.Show();
            }
            catch (Exception)
            { }
        }
        #endregion

        #region Sửa
        private void Sua()
        {
            try
            {
                if (i < 0)
                {
                    return;
                }
                if (dgvHienThi.RowCount == 0)
                {
                    return;
                }
                khuyenmaitanghang = ((Entities.KhuyenMaiTangHang[])dgvHienThi.DataSource)[i];
                if (khuyenmaitanghang == null)
                {
                    MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                GUI.KhuyenMaiTangHang.frmXuLy frm = new GUI.KhuyenMaiTangHang.frmXuLy(khuyenmaitanghang, hienthi);
                frm.GetData = new GUI.KhuyenMaiTangHang.frmXuLy.GetString(GetValue);
                
                frm.Show();
            }
            catch (Exception)
            { }
        }
        #endregion

        #region Xóa
        private void Xoa()
        {
            try
            {
                // check per
                if (!CheckPer(3))
                {
                    MessageBox.Show(Common.MSG.MSG11(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                if (i < 0)
                {
                    return;
                }
                if (dgvHienThi.RowCount == 0)
                {
                    return;
                }

                khuyenmaitanghang = ((Entities.KhuyenMaiTangHang[])dgvHienThi.DataSource)[i];
                // check object
                if (khuyenmaitanghang == null)
                {
                    MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                if (khuyenmaitanghang.GiaTriTrangThai != "Sẽ sử dụng")
                {
                    MessageBox.Show("Không thể xóa chính sách đã hoặc đang sử dụng", Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                // check conflic delete
                if (!CheckConflicDelete())
                {
                    MessageBox.Show(Common.MSG.MSG3(khuyenmaitanghang.MaKhuyenMaiTangHang), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                // check mess
                if (MessageBox.Show(Common.MSG.MSG1(khuyenmaitanghang.MaKhuyenMaiTangHang), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
                {
                    return;
                }
                // check delete
                khuyenmaitanghang.TaiKhoan = UtilitiesVariable.User;
                if (Common.StaticVariable.InterfaceObject.DeleteKhuyenMaiTangHang(khuyenmaitanghang) == 0)
                {
                    MessageBox.Show(Common.MSG.MSG16(khuyenmaitanghang.MaKhuyenMaiTangHang), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                HienThi(Entitystatic.date);
            }
            catch (Exception)
            { }
        }
        #endregion

        #region Check Conflic Delete
        public bool CheckConflicDelete()
        {
            try
            {
                Entities.KhuyenMaiTangHang[] temp = Common.StaticVariable.InterfaceObject.SelectKhuyenMaiTangHang();
                for (int i = 0; i < temp.Length; i++)
                {
                    if (khuyenmaitanghang.MaKhuyenMaiTangHang == temp[i].MaKhuyenMaiTangHang)
                    {
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
        }
        #endregion

        #region Tìm
        public void TimKiem()
        {
            if (rdoMa.Checked)
                dgvHienThi.DataSource = (Entities.KhuyenMaiTangHang[])hienthi.Where(p => p.MaKhuyenMaiTangHang.ToUpper().IndexOf(txtTimKiem.Text.ToUpper()) >= 0).ToArray();
            else
                dgvHienThi.DataSource = (Entities.KhuyenMaiTangHang[])hienthi.Where(p => p.TenDotKhuyenMai.ToUpper().IndexOf(txtTimKiem.Text.ToUpper()) >= 0).ToArray();
            fix();
        }
        #endregion

        #region Thoát
        private void Thoat()
        {
            this.Close();
        }
        #endregion

        #region Event
        private void frmQuanLy_Load(object sender, EventArgs e)
        {
            lblTenNhanVien.Text = UtilitiesVariable.User.TenDangNhap;
            try
            {

                
                btnSua.Click += new EventHandler(btnSua_Click);
                btnThem.Click += new EventHandler(btnThem_Click);
                btnThoat.Click += new EventHandler(btnThoat_Click);
                btnXoa.Click += new EventHandler(btnXoa_Click);
                btnTim.Click += new EventHandler(btnTim_Click);
                txtTimKiem.TextChanged += new EventHandler(txtTimKiem_TextChanged);
                this.FormClosing += new FormClosingEventHandler(frmQuanLy_FormClosing);
                HienThi(Entitystatic.date);
                dgvHienThi.CellClick += new DataGridViewCellEventHandler(dgvHienThi_CellClick);
                dgvHienThi.CellDoubleClick += new DataGridViewCellEventHandler(dgvHienThi_CellDoubleClick);
                dgvHienThi.KeyDown +=new KeyEventHandler(dgvHienThi_KeyDown);
                this.WindowState = FormWindowState.Maximized;
            }
            catch
            {
            }
        }

        void dgvHienThi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                Sua();
            }
        }

        void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            TimKiem();
        }

        void dgvHienThi_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Sua();
        }
        int i = 0;
        void dgvHienThi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            i = e.RowIndex;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            Them();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            Sua();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            Xoa();
        }

        void btnTim_Click(object sender, EventArgs e)
        {
            TimKiem();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }

        private void frmQuanLy_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!checkClose)
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    e.Cancel = checkClose;
                else
                    e.Cancel = !checkClose;
        }
        #endregion
    }
}
#endregion